/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package ims.banking;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BankingFrame extends JFrame {
    private BankingService bankingService;

    private JTextField accountField, amountField;
    private JTextArea infoArea;

    public BankingFrame(BankingService bankingService) {
        this.bankingService = bankingService;

        setTitle("Banking Management");
        setSize(500, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel panel = new JPanel(new BorderLayout());


        JPanel inputPanel = new JPanel(new GridLayout(3, 2));
        inputPanel.add(new JLabel("Account ID:"));
        accountField = new JTextField();
        inputPanel.add(accountField);

        inputPanel.add(new JLabel("Amount:"));
        amountField = new JTextField();
        inputPanel.add(amountField);

        JButton createBtn = new JButton("Create Account");
        JButton depositBtn = new JButton("Deposit");
        JButton withdrawBtn = new JButton("Withdraw");

        inputPanel.add(createBtn);
        inputPanel.add(depositBtn);
        inputPanel.add(withdrawBtn);

        panel.add(inputPanel, BorderLayout.NORTH);


        infoArea = new JTextArea();
        infoArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(infoArea);
        panel.add(scrollPane, BorderLayout.CENTER);


        JPanel bottomPanel = new JPanel();
        JButton balanceBtn = new JButton("Check Balance");
        JButton highTasksBtn = new JButton("Show High-Priority Tasks");
        bottomPanel.add(balanceBtn);
        bottomPanel.add(highTasksBtn);
        panel.add(bottomPanel, BorderLayout.SOUTH);

        add(panel);


        createBtn.addActionListener(e -> {
            String acc = accountField.getText().trim();
            if (acc.isEmpty()) {
                showMsg("Account ID cannot be empty.");
                return;
            }
            bankingService.createAccount(acc, 0);
            showMsg("Account created: " + acc);
        });

        depositBtn.addActionListener(e -> {
            String acc = accountField.getText().trim();
            double amt = parseAmount();
            if (amt < 0) return;
            if (bankingService.deposit(acc, amt)) {
                showMsg("Deposited $" + amt + " into " + acc);
            } else {
                showMsg("Deposit failed.");
            }
        });

        withdrawBtn.addActionListener(e -> {
            String acc = accountField.getText().trim();
            double amt = parseAmount();
            if (amt < 0) return;
            if (bankingService.withdraw(acc, amt)) {
                showMsg("Withdrew $" + amt + " from " + acc);
            } else {
                showMsg("Withdraw failed.");
            }
        });

        balanceBtn.addActionListener(e -> {
            String acc = accountField.getText().trim();
            double balance = bankingService.getBalance(acc);
            if (balance >= 0) {
                showMsg("Balance for " + acc + ": $" + balance);
            } else {
                showMsg("Account not found.");
            }
        });

        highTasksBtn.addActionListener(e -> {
            StringBuilder sb = new StringBuilder("High Priority Tasks:\n");
            for (String task : bankingService.listHighPriorityTasks()) {
                sb.append("- ").append(task).append("\n");
            }
            showMsg(sb.toString());
        });
    }

    private double parseAmount() {
        try {
            return Double.parseDouble(amountField.getText().trim());
        } catch (NumberFormatException e) {
            showMsg("Invalid amount.");
            return -1;
        }
    }

    private void showMsg(String msg) {
        infoArea.setText(msg);
    }
}
